<?php
/**
 * 后台权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/3 0003
 * Time: 下午 4:53
 */

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;
use Validator;

class AdminController extends BaseController
{

    public function permission(Request $request){
        $list = service('Admin')->permissionTree();
        return view('admin.admin.permission',['list'=>$list['data']]);
    }

    public function updatePermission(Request $request){
        $input = $request->all();
        $result = service('Admin')->updatePermissionTree($input);
    }

    public function permissionAdd(Request $request){
        if($request->ajax()){
            $input = $request->all();
            $rules = [
                'display_name' => 'required|max:50',
                'name' => 'required|max:50',
                'controller' => 'required|max:50',
                'method' => 'required|max:50',
            ];
            $msg = [
                'display_name.required' => '权限名称必须',
                'name.required' => '路由名称必须',
                'display_name.max' => '权限名称太长',
                'name.max' => '路由名称太长',
                'controller.required' => '控制器名称必须',
                'controller.max' => '控制器名称太长',
                'method.required' => '方法名必须',
                'method.max' => '方法名太长',
            ];
            $validator = Validator::make($input,$rules,$msg);
            if($validator->fails()){
                return $this->error(1,$validator->errors()->first());
            }
            $result = service('Admin')->permissionAdd($input);
            if($result){
                return $this->success('添加成功',null,url('admin/permission'));
            }else{
                return $this->error(2,'添加失败');
            }
        }else{
            $list = service('Admin')->permissionTree();
            return view('admin.admin.addpermission',['list'=>$list['data']]);
        }
    }

    public function roles(Request $request){

    }

    public function users(Request $request){

    }
}